package cscalendar.classes;

import cscalendar.interfaces.CSCalendarElement;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;

/**
 *
 * @author Muhammad Anis
 */
public class CSEvent implements CSCalendarElement {

    @Override
    public boolean equals(Object obj) {
        CSEvent tempEvent = (CSEvent) obj;
        return tempEvent.getEntryNo().equals(getEntryNo());
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setEntryNo(Integer entryNo) {
        this.entryNo = entryNo;
    }

    public void setPosition(Point2D position) {
        this.position = position;
    }

    public void setDate(Calendar date) {
        this.date = date;
    }

    public void setParticipants(ArrayList<String> participants) {
        this.participants = participants;
    }

    public Integer getEntryNo() {
        return entryNo;
    }

    public Point2D getPosition() {
        return position;
    }

    public Calendar getDate() {
        return date;
    }

    public ArrayList<String> getParticipants() {
        return participants;
    }
    private Integer entryNo;
    private String description;
    private Point2D position;
    private Calendar date;
    private ArrayList<String> participants;

    public CSEvent() {
        entryNo = 0;
        description = "";
        position = new Point2D.Float();
        date = Calendar.getInstance();
        participants = new ArrayList<>();
    }

    public CSEvent(Integer entryNo, String description, Point2D position, Calendar date, ArrayList<String> participants) {
        this.entryNo = entryNo;
        this.description = description;
        this.position = position;
        this.date = date;
        this.participants = participants;
    }

    public CSEvent(CSEvent event) {
        this.entryNo = event.getEntryNo();
        this.description = event.getDescription();
        this.position = (Point2D) event.getPosition().clone();
        this.date = (Calendar) event.getDate().clone();
        this.participants = (ArrayList<String>) event.getParticipants().clone();
    }

    @Override
    public String elementToString() {
        StringBuilder result = new StringBuilder();
        result.append("Entry number : ").append(entryNo).append("\n");
        result.append("Description : ").append(description).append("\n");
        result.append("Position : ").append(position.getX()).append(", ").append(position.getY()).append("\n");
        result.append("Date and time : ").append(date.get(Calendar.YEAR)).append("-").append(date.get(Calendar.MONTH) + 1).append("-").append(date.get(Calendar.DATE)).append("@").append(date.get(Calendar.HOUR_OF_DAY)).append(":").append(date.get(Calendar.MINUTE)).append(":").append(date.get(Calendar.SECOND)).append("\n");
        result.append("Participants : \n");
        for (String participant : participants) {
            result.append("\t").append(participant).append("\n");
        }
        return result.toString();
    }

    @Override
    public CSCalendarElement elementClone() {
        return new CSEvent(this);
    }

    public static class EventComparator implements Comparator<CSEvent> {

        public EventComparator() {
        }

        @Override
        public int compare(CSEvent o1, CSEvent o2) {
            Calendar cal1 = o1.getDate(), cal2 = o2.getDate();
            if (cal1.getTimeInMillis() > cal2.getTimeInMillis()) {
                return -1;
            } else {
                return 1;
            }
        }
    }
}
